package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.mapper.SettleFeeSheetDetailMapper;
import com.ruoyi.cderp.service.SettleFeeSheetDetailService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.SettleFeeSheetDetail;
import com.ruoyi.cderp.mapper.SettleFeeSheetMapper;
import com.ruoyi.cderp.domain.SettleFeeSheet;
import com.ruoyi.cderp.service.ISettleFeeSheetService;

/**
 * 供应商费用单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class SettleFeeSheetServiceImpl implements ISettleFeeSheetService
{
    @Autowired
    private SettleFeeSheetMapper settleFeeSheetMapper;
    @Autowired
    private SettleFeeSheetDetailMapper settleFeeSheetDetailMapper;

    LoginUser loginUser = null;

    /**
     * 查询供应商费用单
     *
     * @param id 供应商费用单主键
     * @return 供应商费用单
     */
    @Override
    public SettleFeeSheet selectSettleFeeSheetById(String id)
    {

        SettleFeeSheet settleFeeSheet= settleFeeSheetMapper.selectSettleFeeSheetById(id);
        List<SettleFeeSheetDetail> list = settleFeeSheetDetailMapper.selectSettleFeeSheetDetail(id);
        settleFeeSheet.setSettleFeeSheetDetailList(list);
        return settleFeeSheet;
    }

    /**
     * 查询供应商费用单列表
     *
     * @param settleFeeSheet 供应商费用单
     * @return 供应商费用单
     */
    @Override
    public List<SettleFeeSheet> selectSettleFeeSheetList(SettleFeeSheet settleFeeSheet)
    {
        return settleFeeSheetMapper.selectSettleFeeSheetList(settleFeeSheet);
    }

    /**
     * 新增供应商费用单
     *
     * @param settleFeeSheet 供应商费用单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSettleFeeSheet(SettleFeeSheet settleFeeSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleFeeSheet.setCreateById(loginUser.getUser().getUserId().toString());
        settleFeeSheet.setCreateBy(loginUser.getUser().getUserName());
        settleFeeSheet.setCreateTime(DateUtils.getNowDate());
        int rows = settleFeeSheetMapper.insertSettleFeeSheet(settleFeeSheet);
        insertSettleFeeSheetDetail(settleFeeSheet);
        return rows;
    }

    /**
     * 修改供应商费用单
     *
     * @param settleFeeSheet 供应商费用单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSettleFeeSheet(SettleFeeSheet settleFeeSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleFeeSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        settleFeeSheet.setUpdateBy(loginUser.getUser().getUserName());
        settleFeeSheet.setUpdateTime(DateUtils.getNowDate());
        settleFeeSheetMapper.deleteSettleFeeSheetDetailBySheetId(settleFeeSheet.getId());
        insertSettleFeeSheetDetail(settleFeeSheet);
        return settleFeeSheetMapper.updateSettleFeeSheet(settleFeeSheet);
    }


    /**
     * 批量删除供应商费用单
     *
     * @param ids 需要删除的供应商费用单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleFeeSheetByIds(String[] ids)
    {
        settleFeeSheetMapper.deleteSettleFeeSheetDetailBySheetIds(ids);
        return settleFeeSheetMapper.deleteSettleFeeSheetByIds(ids);
    }

    /**
     * 删除供应商费用单信息
     *
     * @param id 供应商费用单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleFeeSheetById(String id)
    {
        settleFeeSheetMapper.deleteSettleFeeSheetDetailBySheetId(id);
        return settleFeeSheetMapper.deleteSettleFeeSheetById(id);
    }

    /**
     * 新增供应商费用单明细信息
     *
     * @param settleFeeSheet 供应商费用单对象
     */
    public void insertSettleFeeSheetDetail(SettleFeeSheet settleFeeSheet)
    {
        List<SettleFeeSheetDetail> settleFeeSheetDetailList = settleFeeSheet.getSettleFeeSheetDetailList();
        String id = settleFeeSheet.getId();
        if (StringUtils.isNotNull(settleFeeSheetDetailList))
        {
            List<SettleFeeSheetDetail> list = new ArrayList<SettleFeeSheetDetail>();
            for (SettleFeeSheetDetail settleFeeSheetDetail : settleFeeSheetDetailList)
            {
                settleFeeSheetDetail.setSheetId(id);
                list.add(settleFeeSheetDetail);
            }
            if (list.size() > 0)
            {
                settleFeeSheetMapper.batchSettleFeeSheetDetail(list);
            }
        }
    }
}
